package com.graden.background.service.sysrolefunction.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graden.background.repository.sysrolefunction.SysRoleFunctionMapper;
import com.graden.background.repository.sysrolefunction.entity.SysRoleFunction;
import com.graden.background.service.sysmenufunction.ISysMenuFunctionService;
import com.graden.background.service.sysrolefunction.ISysRoleFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 *  系统角色功能权限服务类
 * </p>
 *
 * @author zjf
 * @since 2019-03-21
 */
@Service
public class SysRoleFunctionServiceImpl extends ServiceImpl<SysRoleFunctionMapper, SysRoleFunction> implements ISysRoleFunctionService {

	@Autowired
	ISysMenuFunctionService iSysMenuFunctionService;

	/**
	 * 插入角色和功能权限关联表
	 */
	@Override
	public void saveFunction(Integer roleId,Integer[] functionIds) {
		SysRoleFunction sysRoleFunction = new SysRoleFunction();
		sysRoleFunction.setId(0);
		sysRoleFunction.setRoleId(roleId);
		sysRoleFunction.setUpdateTime(LocalDateTime.now());
		sysRoleFunction.setCreateTime(LocalDateTime.now());

		for (Integer functionId : functionIds){
			if(!Objects.isNull(functionId)){
				if(iSysMenuFunctionService.getById(functionId) != null) {
					sysRoleFunction.setFunctionId(functionId);
					this.save(sysRoleFunction);
				}
			}
		}
	}
}
